<template>
  <div class="lc-login-container">
    <div class="lc-login-form">
      <div class="lc-login-title">低代码平台</div>
      <div class="lc-login-content">
        <a-form
          layout="vertical"
          :model="form"
          :rules="rules"
          auto-label-width
          @submit="onSubmit"
        >
          <a-form-item label="账号" field="account">
            <a-input
              v-model="form.account"
              placeholder="请输入账号"
              allow-clear
            ></a-input>
          </a-form-item>
          <a-form-item label="密码" field="password">
            <a-input-password
              v-model="form.password"
              placeholder="请输入密码"
              allow-clear
            ></a-input-password>
          </a-form-item>
          <a-form-item
            v-if="!form.isLogin"
            label="确认密码"
            field="confirmPassword"
          >
            <a-input-password
              v-model="form.confirmPassword"
              placeholder="请输入密码"
              allow-clear
            ></a-input-password>
          </a-form-item>
          <a-form-item>
            <div class="flex-between" style="width: 100%">
              <a-checkbox v-model="form.remember">记住密码</a-checkbox>
              <a-link href="/forget">忘记密码</a-link>
            </div>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" html-type="submit" size="large" long
              >登录</a-button
            >
          </a-form-item>
        </a-form>
      </div>
      <div class="lc-login-footer">
        <div class="lc-login-footer__links">
          <a-link @click="onisLogin()">{{
            form.isLogin ? "注册账号" : "登录账号"
          }}</a-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import api from "@/api/login";
import { useUserStore } from "@/store/global";
import { Notification } from "@arco-design/web-vue";
import router from "@/router/index";
const useUser = useUserStore();
const form = ref({
  isLogin: true,
  account: "",
  password: "",
  confirmPassword: "",
  remember: false,
});
const rules = reactive({
  account: [
    {
      required: true,
      message: "账号不能为空",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
    },
  ],
});
const onisLogin = () => {
  form.value.isLogin = !form.value.isLogin;
};
const onSubmit = ({ values, errors }: any) => {
  if (errors) return;
  if (form.value.isLogin) {
    onRegister();
  } else {
    onRegisterAccount();
  }
};
const onRegister = () => {
  let data = {
    account: form.value.account,
    password: form.value.password,
  };
  api.login(data).then((res) => {
    if (res.code == 200) {
      Notification.success(res.msg);
      useUser.userInfo = res.data.user;
      useUser.token = res.data.access_token;
      router.push("/");
    } else {
      Notification.error(res.msg);
    }
  });
};
const onRegisterAccount = () => {
  let data = {
    account: form.value.account,
    password: form.value.password,
    confirmPassword: form.value.confirmPassword,
  };
  api.register(data).then((res) => {
    if (res.code == 200) {
      Notification.success(res.msg);
      useUser.userInfo = res.data.user;
      useUser.token = res.data.access_token;
      router.push("/");
    } else {
      Notification.error(res.msg);
    }
  });
};
</script>

<style lang="scss" scoped>
.lc-login-container {
  background: url(../../assets/image/login.webp) no-repeat center;
  background-size: cover;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .lc-login-form {
    min-width: 400px;
    background: #fff;
    border-radius: 10px;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    .lc-login-title {
      text-align: center;
      margin-bottom: 20px;
      font-size: 24px;
      font-weight: bold;
      color: #333;
      line-height: 1;
      letter-spacing: 2px;
      margin-bottom: 40px;
    }
    .lc-login-input {
      margin-bottom: 20px;
    }
  }
  .lc-login-footer {
    text-align: right;
    font-size: 12px;
    color: #999;
    letter-spacing: 1px;
  }
}
</style>
